home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / PlainTalk / Speech Recognition / Sample Code / Tiny SR Example / CodeWarrior Tiny Example / SpeechRecognition.h next >
Encoding:
C/C++ Source or Header  |  1995-06-22  |  17.6 KB  |  418 lines  |  [TEXT/MPS ]

  1. /************************************************************
  2.  
  3. Created: Monday, January 27, 1992 at 10:19:57 AM
  4.     SpeechRecognition.h
  5.     C Interface to the Apple Speech Recognition Toolbox
  6.  
  7.     Contact:
  8.         Kurt Rodarmer    x2-8054
  9.         Matt Pallakoff    x4-7307
  10.         Arlo Reeves        x4-5440
  11.  
  12.         Copyright Apple Computer, Inc.    1992-1994
  13.         All rights reserved
  14.  
  15. ************************************************************/
  16.  
  17.  
  18. #ifndef        __SPEECHRECOGNITION__
  19. #define        __SPEECHRECOGNITION__
  20.  
  21. #ifndef        __MEMORY__
  22. #include    "Memory.h"
  23. #endif
  24.  
  25. #ifndef    gestaltSpeechRecognitionVersion
  26.  
  27. /* need to put these into GestaltEqu.h */
  28.  
  29. #define    gestaltSpeechRecognitionVersion     'srtb'
  30. #define    gestaltSpeechRecognitionAttr        'srta'
  31.  
  32. #define    gestaltDesktopSpeechRecognition        (1L<<0)
  33. #define    gestaltTelephoneSpeechRecognition    (1L<<1)
  34.  
  35. #endif
  36.  
  37.  
  38. /* Error Codes [Speech recognition gets -5100 through -5199] */
  39.  
  40. enum {
  41.     eSRNotAvailable                = -5100,    /* the service requested is not avail or applicable */
  42.     eSRInternalError            = -5101,    /* a system internal or hardware error condition */
  43.     eSRComponentNotFound        = -5102,    /* a needed system resource was not located */
  44.     eSROutOfMemory                = -5103,    /* an out of memory error occurred in the toolbox memory space */
  45.     eSRNotASpeechObject            = -5104,    /* the object specified is no longer or never was valid */
  46.     eSRBadParameter                = -5105,    /* an invalid parameter was specified */
  47.     eSRParamOutOfRange            = -5106,    /* when we say 0-100, don't pass in 101. */
  48.     eSRBadSelector                = -5107,    /* an unrecognized selector was specified */
  49.     eSRBufferTooSmall            = -5108,    /* returned from attribute access functions */
  50.     eSRNotARecSystem            = -5109,    /* the object used was not a RecognitionSystem */
  51.     eSRFeedbackNotAvail            = -5110,    /* there is no feedback window associated with Recognizer */
  52.     eSRCantSetProperty            = -5111,    /* a non-settable property was specified */
  53.     eSRCantGetProperty            = -5112,    /* a non-gettable property was specified */
  54.     eSRCantSetDuringRecognition    = -5113,    /* the property can't be set while recognition is in progress -- do before or between utterances. */
  55.     eSRAlreadyListening            = -5114,    /* in response to SRStartListening */
  56.     eSRNotListeningState        = -5115,    /* in response to SRStopListening */
  57.     eSRModelMismatch            = -5116,    /* no acoustical models are avail to match request */
  58.     eSRNoClientLanguageModel    = -5117,    /* trying to access a non-specified LanguageModel */
  59.     eSRNoPendingUtterances        = -5118,    /* nothing to continue search on */
  60.     eSRRecognitionAborted        = -5119,    /* an abort error occurred during search */
  61.     eSRRecognitionDone            = -5120,    /* search has finished, but nothing was recognized */
  62.     eSROtherRecAlreadyModal        = -5121,    /* another recognizer is modal at the moment, so can't set this recognizer's kBlockModally property right now */
  63.     eSRHasNoSubItems            = -5122,    /* SRCountItems or related routine was called on an object without subelements -- e.g. a word -- rather than phrase, path, or LM. */
  64.     eSRSubItemNotFound            = -5123,    /* returned when accessing a non-existent sub item of a container */
  65.     eSRLanguageModelTooBig        = -5124,    /* Cant build language models so big */
  66.     eSRAlreadyReleased            = -5125,    /* this object has already been released before */
  67.     eSRAlreadyFinished            = -5126,    /* the language model can't be finished twice */
  68.     eSRWordNotFound                = -5127,    /* the spelling couldn't be found in lookup(s) */
  69.     eSRNotFinishedWithRejection    = -5128,    /* property not found because the LMObj is not finished with rejection */
  70.     eSRExpansionTooDeep            = -5129,    /* Language model is left recursive or is embedded too many levels */
  71.     eSRTooManyElements            = -5130,    /* Too many elements added to phrase or path or other langauge model object */
  72.     eSRCantAdd                    = -5131,    /* Can't add given type of object to the base LMObject (e.g.in SRAddLMObject)    */
  73.     eSRSndInSourceDisconnected    = -5132,    /* Sound input source is disconnected */
  74.     
  75.     /* non-release debugging error codes are included here */
  76.     eSRNotImplementedYet        = -5199        /* you'd better wait for this feature in a future release */
  77. };
  78.  
  79.  
  80. /* Type Definitions */
  81.  
  82. typedef unsigned long SpeechObject;
  83. typedef SpeechObject RecognitionSystem;
  84. typedef SpeechObject Recognizer;
  85. typedef SpeechObject SpeechSource;
  86. typedef SpeechSource RecognitionResult;
  87. typedef SpeechObject LMObject;            /* an LMWord, LMPhrase or LanguageModel */
  88. typedef LMObject LanguageModel;
  89. typedef LMObject LMPath;
  90. typedef LMObject LMPhrase;
  91. typedef LMObject LMWord;
  92. typedef unsigned long LMFlags;
  93. typedef unsigned short SpeedSetting;    /* between 0 and 100 */
  94. typedef unsigned short RejectionLevel;    /* between 0 and 100 */
  95. typedef unsigned long UtteranceLength;    /* a length measure in mS */
  96. typedef unsigned long UtteranceCount;    /* the number of queueable utterances */
  97.  
  98. /* When an event occurs, the user supplied proc will be called with a pointer
  99.     to the param passed in and a flag to indicate conditions such
  100.     as interrupt time or system background time.
  101. */
  102.  
  103. #if defined(powerc) || defined (__powerc)
  104. #pragma options align=mac68k
  105. #endif
  106.  
  107. struct SRCallBackStruct {
  108.     long        what;                /* one of notification flags */
  109.     long        message;            /* contains RecognitionResult id */
  110.     Recognizer    instance;            /* ID of recognizer being notified */
  111.     OSErr        status;                /* result status of last search */
  112.     short        flags;                /* non-zero if occurs during interrupt */
  113. };
  114. typedef struct SRCallBackStruct SRCallBackStruct;
  115.  
  116. #if defined(powerc) || defined(__powerc)
  117. #pragma options align=reset
  118. #endif
  119.  
  120. /* Call back procedure definition */
  121. typedef pascal void (*SRCallBackProcPtr) (SRCallBackStruct *param);
  122.  
  123. #ifndef    USESROUTINEDESCRIPTORS
  124. #define    USESROUTINEDESCRIPTORS 0
  125. #endif
  126.  
  127. #if USESROUTINEDESCRIPTORS
  128. enum {
  129.     uppSRCallBackProcInfo = kPascalStackBased
  130.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(SRCallBackStruct*)))
  131. };
  132.  
  133. typedef UniversalProcPtr SRCallBackUPP;
  134.  
  135. #define CallSRCallBackProc(userRoutine, param)        \
  136.         CallUniversalProc((UniversalProcPtr)(userRoutine), uppSRCallBackProcInfo, (param))
  137. #define NewSRCallBackProc(userRoutine)        \
  138.         (SRCallBackUPP) NewRoutineDescriptor((ProcPtr)(userRoutine), uppSRCallBackProcInfo, GetCurrentISA())
  139. #else
  140. typedef SRCallBackProcPtr SRCallBackUPP;
  141.  
  142. #define CallSRCallBackProc(userRoutine, param)        \
  143.         (*(userRoutine))((param))
  144. #define NewSRCallBackProc(userRoutine)        \
  145.         (SRCallBackUPP)(userRoutine)
  146. #endif
  147.  
  148. #if defined(powerc) || defined (__powerc)
  149. #pragma options align=mac68k
  150. #endif
  151.  
  152. struct SRCallBackParam {
  153.     SRCallBackUPP        callBack;
  154.     SRCallBackStruct    *param;
  155. };
  156. typedef struct SRCallBackParam SRCallBackParam;
  157.  
  158. #if defined(powerc) || defined(__powerc)
  159. #pragma options align=reset
  160. #endif
  161.  
  162.  
  163.  
  164. /* Recognition System Types */
  165. #define kPlainTalkSystem            'ptlk'
  166. #define    kDefaultRecognitionSystemID    ((OSType) 0)
  167.  
  168. /* Recognition System Properties */
  169. #define    kRejectedWord                'rejq'    /* the LMWord used to represent a rejection */
  170. #define kSeparationChars            'spch'    /* separation chars for current dialect setting -- use pointer to SeparationChars struct, defined in Dialect.h */
  171. #define    kFeedbackAndListeningModes    'fbwn'    /* (short) one of kNoFeedbackHasListenModes, kHasFeedbackHasListenModes, kNoFeedbackNoListenModes */
  172.                                             /*  kFeedbackAndListeningModes is only available in version 1.4 and later. */
  173.  
  174. #define    kNoFeedbackNoListenModes        0    /* next allocated recognizer has no feedback window and doesn't use listening modes    */
  175. #define    kHasFeedbackHasListenModes        1    /* next allocated recognizer has feedback window and uses listening modes    */
  176. #define    kNoFeedbackHasListenModes        2    /* next allocated recognizer has no feedback window but does use listening modes    */
  177.  
  178. /* Speech Source Types */
  179. #define    kSRDefaultSpeechSource        ((OSType) 0)
  180.  
  181. /* Speech Source Properties */
  182.  
  183.  
  184. /* Notification via Apple Event or Callback */
  185.  
  186. /* Notification Flags */
  187. #define kNotifyRecognitionBeginning    (1L<<0)    /* recognition can begin. client must now call SRContinueRecognition or SRAbortRecognition */
  188. #define kNotifyRecognitionDone        (1L<<1)    /* recognition has terminated. result (if any) is available. */
  189.  
  190. /* Apple Event selectors */
  191.  
  192. /* AppleEvent message class  */
  193. #define kAESpeechSuite                'sprc'
  194.  
  195. /* AppleEvent message event ids */
  196. #define kAESpeechDone                'srsd'
  197. #define kAESpeechDetected            'srbd'
  198.  
  199. /* AppleEvent Parameter ids */
  200. #define keyRecognizer                  'krec'
  201. #define keySpeechResult                'kspr'
  202. #define keySpeechStatus                'ksst'
  203.  
  204. /* AppleEvent Parameter types */
  205. #define typeRecognizer                'trec'
  206. #define typeSpeechResult            'tspr'
  207.  
  208.  
  209. /* Recognizer Properties */
  210. #define kSpeedVsAccuracyParam        'sped'    /* SpeedSetting between 0 and 100 */
  211.                                             /* 0 means more accurate but slower. */
  212.                                             /* 100 means (much) less accurate but faster. */
  213. #define kSearchStatusParam            'stat'    /* see status flags below */
  214. #define kNotificationParam            'noti'    /* see notification flags below */
  215. #define kCallBackParam                'call'    /* type SRCallBackParam */
  216. #define    kAutoFinishingParam            'afin'    /* automatic finishing applied on LM for search */
  217. #define kForegroundOnly                'fgon'    /* Boolean. Default is true. If true, client recognizer only active when in foreground.    */
  218. #define kBlockBackground            'blbg'    /* Boolean. Default is false. If true, when client recognizer in foreground, rest of LMs are inactive.    */
  219. #define kBlockModally                'blmd'    /* Boolean. Default is false. When true, this client's LM is only active LM; all other LMs are inactive. Be nice, don't be modal for long periods! */
  220. #define kWantsTextFeedback            'txfb'    /* Boolean. Default is true. If true, search results are posted to Feedback window */
  221. #define kWantsDefaultFBResponse        'dfbr'    /* Boolean. Default is true. If true, client needn't call SRProcessBegin/End to get default feedback behavior */
  222.  
  223. /* Operational Status Flags */
  224. #define kIdleRecognizer                (1L<<0)    /* engine is not active */
  225. #define kSearchInProgress            (1L<<1)    /* search is in progress */
  226. #define kSearchWaitForAllClients    (1L<<2)    /* search is suspended waiting on all clients' input */
  227. #define kMustAbortSearch            (1L<<3) /* something has occurred (sound played, non-speech detected) requiring the search to abort */
  228. #define kPendingSearch                (1L<<4) /* we're about to start searching */
  229.  
  230. /* Recognition Result Properties */
  231. #define    kTEXTFormat                    'TEXT'    /* raw text in user supplied memory */
  232. #define kLMPhraseFormat                'lmph'    /* LMPhrase containing result words */
  233. #define kLMPathFormat                'lmpt'    /* LMPath containing result phrases or words */
  234. #define kLanguageModelFormat        'lmfm'    /* top level LanguageModel for post parse */
  235.  
  236. /* LMObject Family Properties */
  237. #define kLMName                        'name'    /* name of a LanguageModel */
  238. #define kSpelling                    'spel'    /* spelling of a LMWord or LMPhrase or LMPath, or name of a LanguageModel */
  239. #define kLMObjType                    'lmtp'    /* Returns one of LMObject Types listed below */
  240. #define kRefCon                        'refc'    /* 4 bytes of user storage */
  241. #define kEnabled                    'enbl'    /* Boolean -- true if LMObject enabled */
  242. #define kOptional                    'optl'    /* Boolean -- true if LMObject is optional    */
  243. #define kRepeatable                    'rptb'    /* Boolean -- true if LMObject is repeatable */
  244. #define kRejectable                 'rjbl'    /* Boolean -- true if LMObject is rejectable (Recognition System's kRejectedWord
  245.                                                 object can be returned in place of LMObject with this property)    */
  246. #define kPrimitive                    'prim'    /* Boolean -- determines what shows up in search result's list of primitives */
  247. #define kRejectionLevel                'rjct'    /* RejectionLevel between 0 and 100 */
  248. #define kFinishingFlags                'finf'    /* Please use kOptional, kRepeatable, and kRejetable instead. pass pointer to LMFlags. Being phased out of public interfaces. */
  249.  
  250.  
  251. /* LM Object Types -- returned as kLMObjType property of language model objects */
  252. #define kLMType                        'lmob'    /* LanguageModel */
  253. #define kPathType                    'path'    /* LMPath */
  254. #define kPhraseType                    'phra'    /* LMPhrase */
  255. #define kWordType                    'word'    /* LMWord */
  256.  
  257. /* a normal and reasonable rejection level */
  258. #define    kDefaultLMRejectionLevel     50
  259.  
  260. /* Finishing Flags - used to make up LMFlags, which is used for kFinishingFlags and kAutoFinishingParam properties */
  261. /* 4/10/95 These finishing flags are being phased out of the public interfaces.  Please use kOptional, kRepeatable,
  262.     and kRejectable Boolean LMObject properties instead.  Those Boolean properties are easier to use.                */
  263.                                             /* bit zero is reserved */
  264. #define kAddPauses                     (1L<<1)    /* allows user to pause between words */
  265. #define kAddRejection                 (1L<<2)    /* unrecognized words will be rejected */
  266. #define kAddNoise                    (1L<<3)    /* allows a some noise at beginning or end of utterance */
  267. #define kMakeOptional                (1L<<4)    /* contents of LM are optional */
  268. #define kMakeRepeatable                (1L<<5)    /* contents of LM may be repeated 0 or more times */
  269. #define kAddWordSpotting            (kAddRejection | kMakeRepeatable)
  270. #define kDefaultFinishing            kAddPauses
  271. #define kNoFinishing                0
  272.  
  273. /*
  274.                         NOTES ON USING THE API
  275.  
  276.     All operations (with the exception of SRGetRecognitionSystem) are
  277.     directed toward an object allocated or begot from New, Get and Read
  278.     type calls.
  279.     
  280.     There is a simple rule in dealing with allocation and disposal:
  281.     
  282.     *    all toolbox allocations are obtained from a RecognitionSystem
  283.     
  284.     *    if you obtain an object via New or Get, then you own a reference 
  285.         to that object and it must be released via SRReleaseObject when
  286.         you no longer need it
  287.     
  288.     *    when you receive a RecognitionResult object via AppleEvent or
  289.         callback, it has essentially been created on your behalf and so
  290.         you are responsible for releasing it as above
  291.     
  292.     *    when you close a RecognitionSystem, all remaining objects which
  293.         were allocated with it will be forcefully released and any
  294.         remaining references to those objects will be invalid.
  295.     
  296.     This translates into a very simple guideline:
  297.         If you allocate it or have it allocated for you, you must release
  298.         it.  If you are only peeking at it, then don't release it.
  299.  
  300. */
  301.  
  302. #if defined (__NOSRTRAPS__)
  303. #define    SRINLINE(hx)    /* eat it up */
  304. #elif defined (THREEWORDINLINE)
  305. #define    SRINLINE(hx)    THREEWORDINLINE(0x303C,hx,0xAA56)
  306. #else
  307. #define    SRINLINE(hx)    = {0x303C,hx,0xAA56}
  308. #endif
  309.  
  310.  
  311. #ifdef    __cplusplus
  312. extern "C" {
  313. #endif
  314.  
  315. /* Opening and Closing of the RecognitionSystem */
  316. pascal OSErr SROpenRecognitionSystem (RecognitionSystem *system, OSType systemID)
  317.  SRINLINE (0x0400);
  318. pascal OSErr SRCloseRecognitionSystem (RecognitionSystem system)
  319.  SRINLINE (0x0201);
  320.  
  321. /* Accessing Properties of any Speech Object */
  322. pascal OSErr SRSetProperty (SpeechObject srObject, OSType selector,
  323.         const void *property, Size propertyLen)
  324.  SRINLINE (0x0802);
  325. pascal OSErr SRGetProperty (SpeechObject srObject, OSType selector,
  326.         void *property, Size *propertyLen)
  327.  SRINLINE (0x0803);
  328.  
  329. /* Any object obtained via New or Get type calls must be released */
  330. pascal OSErr SRReleaseObject (SpeechObject srObject)
  331.  SRINLINE (0x0204);
  332.  
  333. /* Traversing RecognitionResult or LMObjects */
  334. pascal OSErr SRCountItems (SpeechObject container, long *count)
  335.  SRINLINE (0x0405);
  336. pascal OSErr SRGetIndItem (SpeechObject container, SpeechObject *item, long index)
  337.  SRINLINE (0x0606);
  338. pascal OSErr SRSetIndItem (SpeechObject container, SpeechObject item, long index)
  339.  SRINLINE (0x0607);
  340. pascal OSErr SRRemoveIndItem (SpeechObject container, long index)
  341.  SRINLINE (0x0408);
  342.  
  343. /* Selecting the SpeechSource */
  344. pascal OSErr SRGetSpeechSource (RecognitionSystem system, SpeechSource *source,
  345.         OSType selector)
  346.  SRINLINE (0x0609);
  347.  
  348. /* Recognizer Instance Functions */
  349. pascal OSErr SRNewRecognizer (RecognitionSystem system, Recognizer *instance,
  350.         OSType sourceID)
  351.  SRINLINE (0x060A);
  352. pascal OSErr SRNewRecognizerFromSource (RecognitionSystem system, Recognizer *instance,
  353.         SpeechSource source)
  354.  SRINLINE (0x060B);
  355. pascal OSErr SRStartListening (Recognizer instance)
  356.  SRINLINE (0x020C);
  357. pascal OSErr SRStopListening (Recognizer instance)
  358.  SRINLINE (0x020D);
  359. pascal OSErr SRSetLanguageModel (Recognizer instance, LanguageModel active)
  360.  SRINLINE (0x040E);
  361. pascal OSErr SRGetLanguageModel (Recognizer instance, LanguageModel *active)
  362.  SRINLINE (0x040F);
  363. pascal OSErr SRContinueRecognition (Recognizer instance)
  364.  SRINLINE (0x0210);
  365. pascal OSErr SRAbortRecognition (Recognizer instance)
  366.  SRINLINE (0x0211);
  367.  
  368. /* Language Model Building and Manipulation Functions */
  369. pascal OSErr SRNewLanguageModel (RecognitionSystem system, LanguageModel *model,
  370.         const void *name, Size nameLength)
  371.  SRINLINE (0x0812);
  372. pascal OSErr SRNewPath (RecognitionSystem system, LMPath *path)
  373.  SRINLINE (0x0413);
  374. pascal OSErr SRNewPhrase (RecognitionSystem system, LMPhrase *phrase,
  375.         const void *text, Size textLength)
  376.  SRINLINE (0x0814);
  377. pascal OSErr SRNewWord (RecognitionSystem system, LMWord *word,
  378.         const void *text, Size textLength)
  379.  SRINLINE (0x0815);
  380.  
  381. /* Operations on any object of the LMObject family */
  382. pascal OSErr SRSaveLMObject (LMObject lmObject, short resID)
  383.  SRINLINE (0x0316);
  384. pascal OSErr SRLoadLMObject (RecognitionSystem system,
  385.         LMObject *lmObject, short resID)
  386.  SRINLINE (0x0517);
  387. pascal OSErr SREmptyLMObject (LMObject lmObject)
  388.  SRINLINE (0x0218);
  389. pascal OSErr SRChangeLMObject (LMObject lmObject, const void *text, Size textLength)
  390.  SRINLINE (0x0619);
  391. pascal OSErr SRAddLMObject (LMObject base, LMObject addon)
  392.  SRINLINE (0x041A);
  393. pascal OSErr SRAddText (LMObject base, const void *text,
  394.     Size textLength, long refCon)
  395.  SRINLINE (0x081B);
  396. pascal OSErr SRRemoveLMObject (LMObject base, LMObject toRemove)
  397.  SRINLINE (0x041C);
  398.  
  399. /* Utilizing the System Feedback Window */
  400. pascal OSErr SRProcessBegin (Recognizer instance, Boolean fail)
  401.  SRINLINE (0x031D);
  402. pascal OSErr SRProcessEnd (Recognizer instance, Boolean fail)
  403.  SRINLINE (0x031E);
  404. pascal OSErr SRSpeakAndDrawText (Recognizer instance, const void *text, Size textLength)
  405.  SRINLINE (0x061F);
  406. pascal OSErr SRSpeakText (Recognizer instance, const void *speakText, Size speakLength)
  407.  SRINLINE (0x0620);
  408. pascal OSErr SRDrawText (Recognizer instance, const void *dispText, Size dispLength)
  409.  SRINLINE (0x0621);
  410. pascal OSErr SRDrawRecognizedText (Recognizer instance, const void *dispText, Size dispLength)
  411.  SRINLINE (0x0622);
  412.  
  413. #ifdef    __cplusplus
  414. }
  415. #endif
  416.  
  417. #endif    /* __SPEECHRECOGNITION__ */
  418.